package org.wappumobileapp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.thoughtworks.xstream.XStream;

import dataobjects.Message;

public class MyLikes extends BaseActivity {

	private List<Message> messageList;
	private Message suggestion;
	private List<Long> ids = new ArrayList<Long>();
	private SimpleAdapter adapter;
	private ArrayList<Map<String, ?>> list;
	private ListView listView;
	private File f;
			
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.content_history);

		listView = (ListView) findViewById(R.id.myList);

		XStream xstream = new XStream();
		
		f = new File("/data/data/org.wappumobileapp/files/my_likes.xml");
		
		if (f.exists()) {
			try {
				BufferedReader inputReader = new BufferedReader(
						new InputStreamReader(openFileInput("my_likes.xml")));
				ids = (List<Long>) xstream.fromXML(inputReader);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			messageList = GetFromService.getHistory(ids);
			
			if (messageList == null) {
				Toast.makeText(getApplicationContext(), R.string.Problem,
						Toast.LENGTH_LONG).show();
			} else {

				list = buildData(messageList);
				
				String[] from = { "date", "message", "author", "numLikes",
						"numDontKnows", "numDislikes" };
				int[] to = { R.id.date, R.id.message, R.id.author, R.id.LikeResultTextView,
						R.id.DontknowResultTextView, R.id.DislikeResultTextView };
	
				adapter = new SimpleAdapter(this, list,
						R.layout.row_history, from, to);
				// Assign adapter to ListView
				listView.setAdapter(adapter);		
	
				registerForContextMenu(listView);
			}

		} else {
			Toast.makeText(getApplicationContext(), R.string.noSuggestions,
					Toast.LENGTH_SHORT).show();
		}
	}

	private ArrayList<Map<String, ?>> buildData(List<Message> messages) {
		ArrayList<Map<String, ?>> list = new ArrayList<Map<String, ?>>();
		for (int i = 0; i < messageList.size(); i++) {
			suggestion = messageList.get(i);
			list.add(putData(suggestion));
		}
		return list;
	}

	private HashMap<String, Object> putData(Message message) {
		HashMap<String, Object> item = new HashMap<String, Object>();
		item.put("date", suggestion.getDate().toGMTString());
		item.put("message", suggestion.getMessage());
		item.put("author", suggestion.getNickname());
		item.put("numLikes", suggestion.getLikes());
		item.put("numDontKnows", suggestion.getIndifferent());
		item.put("numDislikes", suggestion.getDislikes());
		return item;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.history_tag_likes, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		switch (item.getItemId()) {
		case R.id.deleteLike:
			ids.remove(info.position);
			if (ids.size() == 0){
				f.delete();
			} else {
				writingHistory(ids, "my_likes.xml");
			}
			finish();
			Intent k = new Intent(this, History.class);
			startActivity(k);
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}
}